<?php

declare(strict_types=1);

namespace app\model;

use app\model\base\BaseActivityConfigModel;
use think\model\concern\SoftDelete;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id
 * @property int $act_id 活动id
 * @property string $act_name 原始名称
 * @property string $short_name H5显示名称
 * @property string $type 类型比如Food、Travel
 * @property string $image_url 图片
 * @property int $sort 排序
 * @property bool $status 0 不启用 1启用
 * @property string $channel 渠道比如聚推客
 * @property mixed $ext_data 扩展信息
 * @property string $discount_logo 标识
 * @property string $subtitle 副标题
 * @property int $serv_type 订单业务类型
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $deleted_at 删除时间
 * @method static \think\db\Query onlyTrashed()
 * @method static \think\db\Query withTrashed()
 */
class ActivityConfig extends BaseActivityConfigModel
{
    use SoftDelete;

    // 设置json类型字段
    protected $json = ['ext_data'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    const CHANNEL_JUTUIKE = 'jutuike';

    //status常量
    const STATUS_ACTIVE = 1; //激活
    const STATUS_CLOSE = 0;  //关闭

    //类别
    const TYPE_TRAVEL = 'travel';
    const TYPE_UNION_FOOD = 'union_food';

    //隐藏字段
    protected $hidden = ['deleted_at', 'act_name'];

    /**
     * 根据条件获取聚推客活动列表
     */
    public function getJutuikelList($params)
    {
        return self::query()->where('channel', self::CHANNEL_JUTUIKE)
            ->withSearch(['type', 'act_id', 'short_name', 'serv_type', 'status'], $params)
            ->field($params['field'])
            ->select();
    }

    /**
     * 类别筛选
     */
    public function searchTypeAttr($query, $value, $params)
    {
        $type = $params['type'] ?? '';

        if (!empty($type)) {
            $query->where('type', $type);
        }

    }

    /**
     * act_id筛选
     */
    public function searchActIdAttr($query, $value, $params)
    {
        $act_id = $params['act_id'] ?? '';

        if (!empty($act_id)) {
            $query->where('act_id', $act_id);
        }
    }

    /**
     * 系统品牌名称筛选
     */
    public function searchShortNameAttr($query, $value, $params)
    {
        $short_name = $params['short_name'] ?? '';

        if (!empty($short_name)) {
            $query->where('short_name', 'like', '%'.trim($short_name).'%');
        }
    }

    /**
     * 服务类别筛选
     */
    public function searchServTypeAttr($query, $value, $params)
    {
        $serv_type = $params['serv_type'] ?? '';

        if (!empty($serv_type)) {
            $query->where('serv_type', $serv_type);
        }
    }

    /**
     * 状态筛选
     */
    public function searchStatusAttr($query, $value, $params)
    {
        $status = $params['status'] ?? '';

        if (!empty($status)) {
            $query->where('status', $status);
        }
    }


}
